package com.gdth.bank.adapter;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.bumptech.glide.Glide;
import com.gdth.bank.R;
import com.gdth.bank.bean.DownloadBean;
import com.gdth.bank.model.DownloadModel;
import com.gdth.database.bean.TAppFjxxb;
import com.gdth.model.Item;
import com.gdth.utils.ToolUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CjkhFjxxListViewAdapter extends BaseAdapter {  
	
	private int layout_id;
    private LayoutInflater layoutInflater;  
    private Context context;  
    
 	private List<TAppFjxxb> valList;
 	
 	private List<Item> fjlxList;

    public CjkhFjxxListViewAdapter(Context context, List<TAppFjxxb> valList) {  
        this.context = context;  
        this.valList = valList;
        
        layout_id = R.layout.item_cjkh_fjxx;
    }  

    @Override 
    public int getCount() {  
        return valList.size();  
    }  

    @Override 
    public Object getItem(int position) {  
        return null;  
    }  

    @Override 
    public long getItemId(int position) {  
        return position;  
    }  

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) {  
        Holder holder = null;
        TAppFjxxb item = valList.get(position);
        if (convertView == null) {  
            layoutInflater = LayoutInflater.from(context);  
            convertView = layoutInflater.inflate(layout_id, null);  
            holder = new Holder();  
            holder.txt_fjmc = (TextView) convertView.findViewById(R.id.fjxx_item_fjmc);
            holder.txt_fjlx = (TextView) convertView.findViewById(R.id.fjxx_item_fjlx);
            holder.img = (ImageView) convertView.findViewById(R.id.fjxx_item_img);
            holder.txt_tbbs = (TextView) convertView.findViewById(R.id.fjxx_item_tbbs);
            convertView.setTag(holder);  
        } else {  
            holder = (Holder) convertView.getTag();  
        }  
        if (holder != null) {  
            convertView.setId(position);  
            holder.setId(position);  
            holder.txt_fjmc.setText(item.getWjmc());  
            holder.txt_fjmc.setTag(item.getUuid());
            if(item.getSczt()==0){
            	holder.txt_tbbs.setText(context.getResources().getString(R.string.param263));
            	holder.txt_tbbs.setTextColor(Color.RED);
            }else if(item.getSczt()==3){
            	holder.txt_tbbs.setText(context.getResources().getString(R.string.param264));
            	holder.txt_tbbs.setTextColor(Color.RED);
            }
            else{
            	holder.txt_tbbs.setText(context.getResources().getString(R.string.param265));
            	holder.txt_tbbs.setTextColor(Color.BLACK);
            }
//            else if(item.getSczt()==1){
//            	holder.txt_tbbs.setText("已同步");
//            }else if(item.getSczt()==3){
//            	holder.txt_tbbs.setText("同步失败");
//            	holder.txt_tbbs.setTextColor(Color.RED);
//            }else if(item.getSczt()==3)
            if (fjlxList != null) {
				for (Item mItem : fjlxList) {
					if (mItem.getKey().equals(item.getFjlx().toString())) {
						if(!mItem.getValue().toString().equals("")){
						holder.txt_fjlx.setText(mItem.getValue());
						}else{
							holder.txt_fjlx.setText("");
						}
						break;
					}
				}
			}
            String fullPath = ToolUtil.getFileFullPath(ToolUtil.getFilePath(item.getUuid() + "." +item.getWjlx()));
			//Bitmap bm = null;
//			try {
            File file = new File(fullPath);
            holder.img.setImageResource(R.drawable.bank_tx);
            if(file.exists()) {
            	Glide.with(context).load(fullPath).into(holder.img);
            }else {
            	Bitmap bm = BitmapFactory.decodeFile(fullPath);
            	if (bm == null) {
    				DownloadBean bean = new DownloadBean();
    				bean.setUuid(item.getUuid());
    				bean.setLocalPath(item.getAppdz());
    				 // 2019 -- 6 -11 xjc
    				try {
    					bean.setGnmk(item.getGnmk());	
    				}catch(NullPointerException e) {
    					bean.setGnmk(1);	
    				}
    				
    				bean.setCjsj(new Date());
    				DownloadModel.getInstance().download(bean, holder.img, handler);
    			}
            	
            	         	
            }
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
		
	    }
        return convertView;  
    }  
    
    public class Holder {  
    	public TextView txt_fjmc;   
    	public TextView txt_fjlx;
    	public ImageView img;
    	public TextView txt_tbbs;
    	
    	
    	void setId(int position) {  
    		txt_fjmc.setId(position);    
    	}  
    }
    
    public void setFjlx(List<Item> fjlxList) {
		this.fjlxList = fjlxList;
	}
    
    Handler handler = new Handler(new Handler.Callback() {
		
		@Override
		public boolean handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
				case 101:
					notifyDataSetChanged();
					break;
			}
			
			return false;
		}
	});

}
